#region Usings

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using KanbanBoard.Server.Command.DomainModel;

#endregion

namespace KanbanBoard.Tests.Server.Utils.DSL {
    public static class BoardExtensions {
        public static List<Column> Columns(this TaskBoard board) {
            var columns = (IEnumerable<object>) typeof(TaskBoard).GetProperty("Columns", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(board, null);
            return columns.Cast<Column>().ToList();
        }

        public static Column Column(this TaskBoard board, string columnName) {
            return (from column in board.Columns()
                    where column.Name() == columnName
                    select column)
                .FirstOrDefault();
        }

        public static Task Task(this TaskBoard board, string taskDescription) {
            return (from column in board.Columns()
                    from task in column.Tasks()
                    where task.Description() == taskDescription
                    select task)
                .FirstOrDefault();
        }

        public static List<Task> Tasks(this Column column) {
            var columns = (IEnumerable<object>)typeof(Column).GetProperty("Tasks", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(column, null);
            return columns.Cast<Task>().ToList();
        }

        public static string Name(this Column column) {
            return (string)typeof(Column).GetProperty("Name", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(column, null);
        }

        public static string Description(this Task task) {
            return (string)typeof(Task).GetProperty("Description", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(task, null);
        }
    }
}